package example.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.fs.permission.FsPermission;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.net.URI;
import java.util.Arrays;

public class HdfsTest {

    private FileSystem fileSystem = null;

    @Before
    public void before() throws Exception {
        URI uri = new URI("hdfs://hadoop201:8020");
        Configuration configuration = new Configuration();
        configuration.set("dfs.replication", "1");
        fileSystem = FileSystem.get(uri, configuration, "lzj");
    }

    @After
    public void after() throws Exception {
        fileSystem.close();
    }


    @Test
    public void mkdir() throws Exception {
        fileSystem.mkdirs(new Path("/xiyouji/huaguoshan"));
    }

    /**
     * 上传
     * classpath下的hdfs-site配置优先级高于默认和集群配置,
     * 代码configuration设置比classpath下的hdfs-site配置优先级高
     */
    @Test
    public void put() throws Exception {
        //
        fileSystem.copyFromLocalFile(true, true, new Path("D:\\2.txt"), new Path("/xiyouji/jh2.txt"));
        //fileSystem.copyFromLocalFile(new Path("/xiyouji/huaguoshan"), new Path(""));
    }


    /**
     * 下载文件
     */
    @Test
    public void get() throws Exception {
        // copyToLocalFile(源文件是否删除 hdfs路径 本地路径 是否校验文件)
        fileSystem.copyToLocalFile(true, new Path("/xiyouji/jh2.txt"), new Path("D:\\2.txt"), false);
    }


    @Test
    public void delete() throws Exception {
        // 删除的路径 是否递归(删除非空目录需要递归)
        fileSystem.delete(new Path("/xiyouji/jh2.txt"), true);
    }

    /**
     * 文件移动 目录更名
     */
    @Test
    public void rename() throws Exception {
        // 源文件 目标文件
        fileSystem.rename(new Path("/xiyouji/jh.txt"), new Path("/xiyouji/jh6.txt"));
    }


    /**
     * 获取文件信息
     */
    @Test
    public void detail() throws Exception {
        RemoteIterator<LocatedFileStatus> iterator = fileSystem.listFiles(new Path("/"), true);
        while (iterator.hasNext()) {
            LocatedFileStatus fileStatus = iterator.next();
            System.out.println(fileStatus.getPath());
            System.out.println(fileStatus.getPermission());
            System.out.println(fileStatus.getOwner());
            System.out.println(fileStatus.getGroup());
            System.out.println(fileStatus.getLen());
            System.out.println(fileStatus.getModificationTime());
            System.out.println(fileStatus.getReplication());
            System.out.println(fileStatus.getBlockSize());

            BlockLocation[] blockLocations = fileStatus.getBlockLocations();
            System.out.println(Arrays.toString(blockLocations));
            System.out.println("==========================");
            if (fileStatus.isFile()) {
                System.out.println("文件" + fileStatus.getPath().getName());
            }
            if (fileStatus.isDirectory()) {
                System.out.println("目录" +fileStatus.getPath().getName());
            }
        }
    }



}
